﻿using System;
using System.Collections.Generic;
using KuiHuaBaoDian.Services.Battle.Data;
using KuiHuaBaoDian.Services.Battle.Entity;
using KuiHuaBaoDian.Services.Lockstep;

namespace KuiHuaBaoDian.Services.Battle.Behaviour {

    /// <summary>
    /// 闲置行为
    /// </summary>
    public interface IBattleBehaviour_Idle : IBattleBehaviour {

        /// <summary>
        /// 实体
        /// </summary>
        new public interface IEntity : IBattleBehaviour.IEntity {

            /// <summary>
            /// CD经过的时间
            /// </summary>
            IBattleData CoolDownDuration { get; }

            /// <summary>
            /// 如果不为 <c>null</c> ，那么会用队列中的值对<see cref="CoolDownDuration"/>进行运算
            /// </summary>
            Queue<F64> FillQueue { get; }
        }
    }

    [Implement(typeof(IBattleBehaviour_Idle))]
    internal sealed class KHBD_BattleBehaviour_Idle : KHBD_BattleBehaviour, IBattleBehaviour_Idle {

        new private IBattleBehaviour_Idle.IEntity Entity => (IBattleBehaviour_Idle.IEntity)base.Entity;

        protected override void Initialize() {
        }

        protected override IBattleBehaviourInfo LogicFrame() {
            if (DFR_BattleBase.DataCenter.Models.IsCoolDownCanElapse && Entity.CoolDownDuration != null) {
                if (Entity.FillQueue == null) {
                    var deltaTime = LockstepService.Instance.FrameLength;
                    Entity.CoolDownDuration.Value += deltaTime;
                } else {
                    while (Entity.FillQueue.Count > 0) {
                        var fill = Entity.FillQueue.Dequeue();
                        Entity.CoolDownDuration.Value += fill;
                    }
                }
            }

            return new KHBD_BattleBehaviourInfo {
                Type = BattleBehaviourType.Idle,
            };
        }

        protected override void Execute(IBattleBehaviourInfo info) {
        }

        protected override void Update(IBattleBehaviourInfo info, float interpolation) {
            if (StateMachine != null) {
                StateMachine.State = BattleEntityState.Idle;
            }
        }
    }
}
